package com.lib.controller.agent;

import com.alibaba.fastjson.JSON;
import com.lib.application.RentalAgentService;
import com.lib.dto.RentalAgentApplyDTO;
import com.lib.handler.agent.RentalAgentSubscribeHandler;
import com.lib.model.RentalAgent;
import com.lib.model.enums.RentalAgentStatusEnum;
import com.lib.service.WxAgentService;
import me.chanjar.weixin.mp.bean.result.WxMpOAuth2AccessToken;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;

/**
 * 微信入口的controller
 * Created by duqingxiang on 17/5/23.
 */
@Controller
@RequestMapping("/agent")
public class RentalAgentWxController {

    protected Logger logger = LoggerFactory.getLogger(this.getClass());

    @Autowired
    WxAgentService wxService;
    @Autowired
    RentalAgentService rentalAgentService;


    @RequestMapping("/apply/{shopId}")
    public ModelAndView apply(@PathVariable Long shopId
                        , @RequestParam String openId
                        , @RequestParam String shopName){

        ModelAndView view = new ModelAndView();

        RentalAgent agent = rentalAgentService.getRentalAgentByOpenId(openId);
        if (agent != null && agent.getStatus() == RentalAgentStatusEnum.APPLY.getId()) {
            //审核中状态直接跳转至提交成功页面
            view.setViewName("wxShow/agent/agent_success");
            return view;
        }

        RentalAgentApplyDTO applyDTO = RentalAgentApplyDTO.builder()
                .shopId(shopId)
                .shopName(shopName)
                .openId(openId)
                .build();

        view.addObject("apply",applyDTO);
        view.setViewName("wxShow/agent/agent_req");
        return view;
    }

    @RequestMapping("/apply/view/{openId}")
    public ModelAndView apply(@PathVariable String openId){

        ModelAndView view = new ModelAndView();

        RentalAgent agent = rentalAgentService.getRentalAgentByOpenId(openId);
        if (agent == null || agent.getStatus() == RentalAgentStatusEnum.NEW.getId()) {
            //审核中状态直接跳转至提交成功页面
            view.setViewName("wxShow/agent/agent_error");
            return view;
        }

        RentalAgentApplyDTO applyDTO = RentalAgentApplyDTO.builder()
                .shopName(agent.getShopName())
                .name(agent.getName())
                .phone(agent.getPhone())
                .staffId(agent.getStaffId())
                .build();

        view.addObject("apply",applyDTO);
        view.setViewName("wxShow/agent/agent_view");
        return view;
    }

    @RequestMapping("/apply_success")
    public String applySuccess(){

        return "wxShow/agent/agent_success";
    }

}
